package com.svms.service.bo;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.svms.service.constants.SvmsStatusCodes;
import com.svms.service.dml.iface.VehicleDMLIF;
import com.svms.service.iface.VehicleIF;
import com.svms.service.search.SearchOperators;
import com.svms.service.validators.VehicleValidator;
import com.svms.service.vo.ResultCodeVO;
import com.svms.service.vo.VehicleVO;

/**
 * @author
 * 
 */
public class VehicleBO implements VehicleIF {

	VehicleDMLIF vehicleDML;

	public VehicleDMLIF getVehicleDML() {
		return vehicleDML;
	}

	public void setVehicleDML(VehicleDMLIF vehicleDML) {
		this.vehicleDML = vehicleDML;
	}

	@Override
	public VehicleVO insertNewVehicle(VehicleVO vehicle) {

		VehicleVO vehicleVO = VehicleValidator.validateVehicle(vehicle);

		if (vehicleVO.getResultCode().getErrorDetails().size() == 0) {
			if (!VehicleValidator.isDuplicateVehicle(vehicleVO)) {
				vehicleVO.setResultCode(new ResultCodeVO());
				vehicleVO = vehicleDML.insertNewVehicle(vehicleVO);
			} else {
				ResultCodeVO resultCodeVO = new ResultCodeVO();
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.VEHILCE_ALREADY_PRESENT);
				vehicleVO.setResultCode(resultCodeVO);
			}
		}
		return vehicleVO;
	}

	@Override
	public VehicleVO editVehicle(VehicleVO vehicle) {
		VehicleVO vehicleVO = VehicleValidator.validateVehicle(vehicle);
		if (vehicleVO.getResultCode().getErrorDetails().size() == 0) {
			vehicleVO = vehicleDML.editVehicle(vehicleVO);
		}
		return vehicleVO;
	}

	@Override
	public VehicleVO deleteVehicle(VehicleVO vehicle) {
		if (vehicle != null && StringUtils.isNotBlank(vehicle.getVehicleId())) {
			vehicle = vehicleDML.deleteVehicle(vehicle);
		} else {
			ResultCodeVO resultCodeVO = new ResultCodeVO();
			resultCodeVO
					.getErrorDetails()
					.add(SvmsStatusCodes.VEHICLE_SHOULD_BE_SELECTED_TO_DELETE_VEHICLE);
		}
		return vehicle;
	}

	@Override
	public VehicleVO getVehicle(VehicleVO vehicle) {
		if (vehicle != null
				&& (StringUtils.isNotBlank(vehicle.getVehicleId()) || StringUtils
						.isNotBlank(vehicle.getVehicleNumber()))) {
			vehicle = vehicleDML.getVehicle(vehicle);
		} else {
			ResultCodeVO resultCodeVO = new ResultCodeVO();
			resultCodeVO.getErrorDetails().add(
					SvmsStatusCodes.VEHICLE_ID_MANDATORY_TO_SELECT_VEHICLE);
		}

		return vehicle;
	}

	@Override
	public List<VehicleVO> getVehicleList() {
		List<VehicleVO> vehicles = vehicleDML.getVehicleList();
		return vehicles;
	}

	@Override
	public List<VehicleVO> searchVehicle(VehicleVO vehicle,
			Map<String, SearchOperators> operatorsList) {
		List<VehicleVO> vehicles = vehicleDML.searchVehicle(vehicle,
				operatorsList);
		return vehicles;
	}

}
